package com.hig.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hig.utils.TreeUtils;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hig.system.domain.TempUserChoice;
import com.hig.system.domain.dto.SysUserData;
import com.hig.system.mapper.SysUserDataMapper;
import com.hig.system.service.ISysUserDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/system/userdata")
public class SysUserDataController extends BaseController{
	@Autowired
	ISysUserDataService sysUserDataService;
    @Autowired
    ISysDeptService sysDeptService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询用户选择列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserData sysUser)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Integer deptId = sysUser.getDeptId();
        List<Long> longs = new ArrayList<>();
        if (ObjectUtil.isNull(deptId)){
            deptId = loginUser.getUser().getDeptId().intValue();
        }

        if (ObjectUtil.isNotNull(deptId)) {
            longs = sysDeptService.selectAllChildDeptIds(deptId.longValue());
        }
        if (CollectionUtil.isNotEmpty(longs)){
            sysUser.setDeptIdList(longs);
            sysUser.setDeptId(null);
        }
        startPage();
        List<SysUserData> list = sysUserDataService.selectUserList(sysUser);
        return getDataTable(list);
    }
    
    @GetMapping("/choicelist")
    public TableDataInfo choiceList(SysUserData sysUser)
    {
        startPage();
        List<SysUserData> list = sysUserDataService.selectChoiceUserList(sysUser);
        return getDataTable(list);
    }
}
